const app= getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showStore:0,
    showRentType:0,
    rentType:1,
    city_id:2,//1483 菏泽，1412 济宁
    bg1:app.globalData.imgUrl+'/bg/bg1.png',
    bg2:app.globalData.imgUrl+'/bg/bg2.png',
    parter:app.globalData.imgUrl+'/bg/parter.png',
    rentDate:12
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      let that  = this;
    var deviceInfo = wx.getDeviceInfo(); 
    var startDate;
    var endDate;

    var date = new Date();
    var year = date.getFullYear();
    console.log(year);
    var nextYear = year +1;
    var month = date.getMonth()+1;
    if(month<10){
      month = "0"+month;
    }
    var day = date.getDate();
    if(day<10){
      day = "0" + day
    }
    startDate = year+"-"+month+"-"+day;
    endDate = nextYear + "-"+month+"-"+day;
    this.setData({
      year:year,
      month:month,
      day:day,
      nextYear:nextYear,
    })  
    let d = app.globalData;
      that.setData({
          width:d.windowWidth,
          height:d.windowHeight,
          statusBarHeight:d.statusBarHeight,
          navHeight:d.navHeight,
          marginTop:d.navTitleTop,
          bottom:d.bottom,
          domain:d.domain,
         imgUrl:d.imgUrl,
          platform:deviceInfo.platform
        })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this;
    var  tab = wx.getStorageSync('tabBar');
   
   if(tab == '' || tab == undefined){
     app.getMenu();
     setTimeout(function(){ 
     //console.log("延时1秒后执行");
       that.getTabBar().init('storeWait');
   }, 1000);
   }else{
     this.getTabBar().init('store');
   } 
    this.getCity();

  },
  startDate(){
    let d = this.data;
    wx.navigateTo({
      url: 'calendar?rentType='+d.rentType,
    })
  },
  toRent(e){
      console.log(e)
      let d = this.data;
      wx.navigateTo({
          url:'param?city_id='+d.city_id+'&rentType='+d.rentType + '&year='+d.year+'&month='+d.month+'&day='+d.day+'&nextYear='+d.nextYear
      })
  },
  getCity(){
    let that = this;
    var city = wx.getStorageSync('city');
    if(city== undefined || city  == ''){
      wx.request({
        url: app.globalData.domain+'/wechat/home/getCity',
        data:{from:1},
        success:res=>{
            console.log(res)
            var data=res.data;
            var model=[];
            var models=data.models;
            console.log(data);
    
            that.setData({
            city:data.list,
            company_img:data.company_img
        
            })
        }
    })
    } else {
      this.setData({
        city:city
      })
    }
    var rent_type = wx.getStorageSync('rent_type');
    if(rent_type == undefined || rent_type  == ''){
      wx.request({
        url: app.globalData.domain+'/wechat/home/getRentType',
        data:{from:1},
        success:res=>{
            console.log(res)
            var d=res.data;
        
            console.log(d);
            wx.getStorageSync('rent_type',d.rent_type)
            that.setData({
            rent_type:d.rent_type,
      
        
            })
        }
    })
    }else{
      this.setData({
        rent_type:rent_type
      })
    }
  },
  showStore(){
      var maskTitle ='切换城市';
      var maskHeight=0;
      maskHeight = 160;
     
   
      this.setData({
          showStore:1,
          maskTitle:maskTitle,
          maskHeight:maskHeight
      })
  },
  closeStore(){
      this.setData({
          showStore:0
      })
  },
    showRentType(){
      var maskTitle ='租车周期';
      var maskHeight=0;
      maskHeight = 160;
      let that = this;
 
      this.setData({
          showRentType:1,
          maskTitle:maskTitle,
          maskHeight:maskHeight
      })
  },
  closeRentType(){
      this.setData({
          showRentType:0
      })
  },
  setCity(e){
   console.log(e);
      var id = e.currentTarget.dataset.city_id;
      wx.setStorageSync('city_id', id)
      this.setData({
          city_id:id,
          showStore:0
      })
  },
  setRentType(e){
      console.log(e);
      var rentType = e.currentTarget.dataset.renttype;
      var rentDate;
      wx.setStorageSync("rentType",rentType)
      var nextYear;
      let d = this.data;
      var year = d.year ;
      var rent_type = d.rent_type;
     rent_type.forEach(function(key,value){
       console.log(key)
       if(rentType==key.id){
         nextYear = d.year + key.year;
       }
     });
      
      this.setData({
          rentType:rentType,

          nextYear:nextYear,
          showRentType:0
      })
  },
  goto(e){
      console.log(e)
      var v= e.currentTarget.dataset;
      var url = v.url;
      var index = v.index;
      app.goto(url,index);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})